﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QueueUnlimited.API;
using MultiplayerDice.Implementation.Command;
using System.Transactions;

namespace MultiplayerDice.Implementation.Processor
{
    internal static class GameCommandsProcessor
    {
        public static void Process(GenericEvent genericEvent)
        {
            var startedCommand = genericEvent.IsOfTypeOrNull<NewGameStartedCommand>();
            var endedCommand = genericEvent.IsOfTypeOrNull<ExistingGameEndedCommand>();

            if (startedCommand != null)
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
                using (var domainDataStore = new DomainDataStoreDataContext())
                {
                    if (domainDataStore.Games.Where(x => x.GameId == startedCommand.GameId)
                        .Count() == 0)
                    {
                        domainDataStore.Games.InsertOnSubmit(
                            new Game()
                            {
                                GameId = startedCommand.GameId,
                                GameStarted = startedCommand.DateTimeOfStart,
                                Player1Diced = startedCommand.Diced,
                                Player1Name = startedCommand.Player,
                                Player2Name = startedCommand.OpponentPlayer
                            });
                        domainDataStore.SubmitChanges();

                        EventRaiser.Raise(new BusinessEvent.NewGameStartedEvent()
                        {
                            DateTimeOfStart = startedCommand.DateTimeOfStart,
                            Diced = startedCommand.Diced,
                            GameId = startedCommand.GameId,
                            OpponentPlayer = startedCommand.OpponentPlayer,
                            Player = startedCommand.Player
                        });
                    }
                };
            }
            else if (endedCommand != null)
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
                using (var domainDataStore = new DomainDataStoreDataContext())
                {
                    if (domainDataStore
                        .Games
                        .Where(x => x.GameId == endedCommand.GameId
                            && x.Player2Name == null
                        ).Count() == 0)
                    {
                        var game = domainDataStore.Games.Where(x => x.GameId == endedCommand.GameId).First();
                        game.Player2Diced = endedCommand.Diced;
                        game.GameEnded = DateTime.Now;

                        domainDataStore.SubmitChanges();

                        EventRaiser.Raise(new BusinessEvent.ExistingGameEndedEvent()
                        {
                            DateTimeOfGameEnded = game.GameEnded.Value,
                            DateTimeOfGameStarted = game.GameStarted,
                            GameId = game.GameId,
                            Player1Diced = game.Player1Diced,
                            Player2Diced = game.Player2Diced,
                            Player1Name = game.Player1Name,
                            Player2Name = game.Player2Name
                        });
                    }
                    else
                    {
                        // Throw warning message to user!
                    }
                }
            }
        }
    }
}
